package com.book.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

//短信验证码工具
public class MsgUtils {

    public static void sendMsg(String phone, String code){
        // 以下2处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
        String accessKeyId = "替换成自己的";
        String accessSecret = "替换成自己的";

        //初始化acsClient,暂不支持region化
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");  //产品域名,开发者无需替换
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "灵沉书城");   //必填:短信签名-可在短信控制台中找到
        request.putQueryParameter("TemplateCode", "SMS_187941966");  //必填:短信模板-可在短信控制台中找到
        request.putQueryParameter("TemplateParam", "{'code':'" + code + "'}"); //模板中的变量替换JSON串,如模板内容为您的验证码为${code}"时,此处的值
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
